package edu.fsu.cs.SHPE;

import java.net.ConnectException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ConferenceExpandableListAdapter extends BaseExpandableListAdapter implements OnClickListener {

	private ConferenceContainer conferences;
	private Conference tmpConference;
	
	private Context context;
	private final static String TAG = "SHPE.ConferenceExpandableListAdapter";
	

	public ConferenceExpandableListAdapter(Context context, String url) throws ConnectException{
		this.context = context;
		conferences = new ConferenceContainer(url);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		LayoutInflater inFlate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		View v = inFlate.inflate(R.layout.actionbuttons, null);

		
		tmpConference = conferences.get(groupPosition);
		
		ImageButton btn = (ImageButton) v.findViewById(R.id.btnView);
		btn.setTag(tmpConference);
		btn.setOnClickListener(this);
		
		btn = (ImageButton) v.findViewById(R.id.btnWeb);
		btn.setTag(tmpConference);
		btn.setOnClickListener(this);
		
		btn = (ImageButton) v.findViewById(R.id.btnMap);
		btn.setTag(tmpConference);
		btn.setOnClickListener(this);
		
		btn = (ImageButton) v.findViewById(R.id.btnEmail);
		btn.setTag(tmpConference);
		btn.setOnClickListener(this);
		
		btn = (ImageButton) v.findViewById(R.id.btnTwitter);
		btn.setTag(tmpConference);
		btn.setOnClickListener(this);
		
		
		/*
		LayoutInflater inFlate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		View v = inFlate.inflate(R.layout.expandedconference, null);

		Conference tmpConference = conferences.get(groupPosition);

		TextView tmp = (TextView)v.findViewById(R.id.txtWebsite);
		tmp.setText(tmpConference.getWebsite());
		Linkify.addLinks(tmp, Linkify.WEB_URLS);

		tmp = (TextView)v.findViewById(R.id.txtEmail);
		tmp.setText(tmpConference.getEmail());
		Linkify.addLinks(tmp, Linkify.EMAIL_ADDRESSES);

		tmp = (TextView)v.findViewById(R.id.txtAddress);
		tmp.setText(tmpConference.getAddress() + ',' + tmpConference.getCity() + ',' + tmpConference.getProvince() + ',' +
				tmpConference.getPostalCode());
		Linkify.addLinks(tmp, Linkify.MAP_ADDRESSES);
		 */

		return v;
	}
	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}
	@Override
	public Object getGroup(int groupPosition) {
		return conferences.get(groupPosition);
	}
	@Override
	public int getGroupCount() {
		return conferences.size();
	}
	@Override
	public long getGroupId(int groupPosition) {
		return conferences.get(groupPosition).getId();
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		Log.i("getView", "position " + groupPosition);

		LayoutInflater inFlate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		View v = inFlate.inflate(R.layout.conferenceitem, null);

		tmpConference = conferences.get(groupPosition);

		TextView tmpText = (TextView)v.findViewById(R.id.txtName);
		tmpText.setText(tmpConference.getName());

		tmpText = (TextView)v.findViewById(R.id.txtStartDate);
		tmpText.setText(tmpConference.getStartDate() + " -");

		tmpText = (TextView)v.findViewById(R.id.txtEndDate);
		tmpText.setText(tmpConference.getEndDate());

		tmpText = (TextView)v.findViewById(R.id.txtDescription);
		tmpText.setText(tmpConference.getDescription());

		ImageView img = (ImageView)v.findViewById(R.id.imgDisplay);

		if(tmpConference.getImage() != null){
			img.setImageDrawable(tmpConference.getImage());
			img.setVisibility(View.VISIBLE);

		}

		//img.setId((int)tmpConference.getId());

		return v;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		Log.i("onclick", "button " + v.getId() + " conference " + tmpConference.getId());
		
		Conference selectedConference = (Conference) v.getTag();
		
		if(viewId == R.id.btnView)
			startConferenceDisplayActivity((int)selectedConference.getId());
		else if(viewId == R.id.btnMap)
			startMapActivity(selectedConference.getLat(), selectedConference.getLon(), (int)selectedConference.getId());
		else if(viewId == R.id.btnEmail)
			startEmailActivity(selectedConference.getEmail());
		else if(viewId == R.id.btnWeb)
			startWebActivity(selectedConference.getWebsite());
		else if(viewId == R.id.btnTwitter)
			startWebActivity("http://www.twitter.com/" + selectedConference.getTwitter());
	}
	
	

	private void startEmailActivity(String address){
		Intent intent = new Intent(Intent.ACTION_SEND); 
		String[] tos = { address }; 
		intent.putExtra(Intent.EXTRA_EMAIL, tos);
		//intent.putExtra(Intent.EXTRA_CC, ccs); 
		//intent.putExtra(Intent.EXTRA_TEXT, "body");
		//intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
		intent.setType("message/rfc882"); 
		Intent.createChooser(intent, "Choose Email Client");
		context.startActivity(intent);
	}
	
	private void startMapActivity(String lat, String lon, int id){
		Log.i(TAG, "startingVenueMap Activity");
		
		//Intent intent = new Intent("edu.fsu.cs.SHPE.VenuMapActivity");
		Intent intent = new Intent(context, VenuMapActivity.class);
		intent.setAction(Intent.ACTION_VIEW);
		intent.putExtra("lat", Double.parseDouble(lat));
		intent.putExtra("lon", Double.parseDouble(lon));
		intent.putExtra("id", id);
		intent.putExtra("zoom", Integer.parseInt(context.getString(R.string.zoomfar)));
		//intent.setData(Uri.parse("geo:" + lat + "," + lon));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	
	private void startWebActivity(String url){
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
		
	}
	
	private void startConferenceDisplayActivity(int id){
		Intent intent  = new Intent(context, ConferenceDisplayActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("id", id);
		context.startActivity(intent);
		
	}
	
	

}